//获取地址栏传来的参数
var flag = 0;
var url = "/staff/FindStaffByType";
var response = []; // 储存返回的数据，是一个json列表
var staffs = []; // 储存所有员工姓名
//虚拟数据模版，假数据
var templateBug = [5, 6, 1, 3, 5, 6, 1, 3,5, 6, 1, 3,5, 6, 1, 3,5, 6, 1, 3,5, 6, 1, 3,5, 6, 1, 3,5, 6, 1, 3,5, 6, 1, 3,5, 6, 1, 3,5, 6, 1];
//各项指标
var newBug = [];
var resolveBug = [];
var openBug = [];
var inProcessBug = [];
var reopenBug = [];
var closedBug = [];

newBug = [5, 6, 1, 3, 5, 6, 1, 3,5, 9,1 ];
resolveBug = [5, 6, 1, 3, 5, 6, 1, 3,5, 9, 2];
openBug = [5, 6, 1, 3, 5, 6, 1, 3,5, 9, 2];
inProcessBug = [5, 6, 1, 3, 5, 6, 1, 3,5, 9, 2];
reopenBug = [5, 6, 1, 3, 5, 6, 1, 3,5, 9, 2];
closedBug = [5, 6, 1, 3, 5, 6, 1, 3,5, 9, 2];
//各项指标;

function search() {
    staffs = [];
    var options = $("#department_select option:selected");
    var type = options.text();
    document.getElementById("box_center").style.display = "block";
    document.getElementById("highchart").style.display = "block";
    document.getElementById("box_bottom2").style.display = "none";
    document.getElementById("box_bottom").style.display = "block";
    if (type == "所有端别"){
        $.ajax({
            url: "/staff/FindAllStaff",
            method: 'post',
            contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
            cache: false,
            async: false,
            success: function (data) {
                response = data;
                for (var i = 0; i < response.length; i++) {
                    staffs.push(response[i]["staffName"]);
                }
                console.log("type", type);
                console.log("response", response);
                console.log("staffs", staffs);
            }
        });
    }else {
        $.ajax({
            url: url,
            method: 'post',
            contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
            cache: false,
            async: false,
            data: {type: type},
            success: function (data) {
                response = data;
                for (var i = 0; i < response.length; i++) {
                    staffs.push(response[i]["staffName"]);
                }
                console.log("type", type);
                console.log("response", response);
                console.log("staffs", staffs);
            }
        });
    }
    flag = 1;

    // $("#show_table").bootstrapTable('destroy');
    // $('#show_table').bootstrapTable({
    //     url: url,
    //     method: 'post',
    //     contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
    //     data: {type: type},
    //     async: false,
    //     cache: false,
    //     striped: true,
    //     pagination: true,
    //     // sortable: true,
    //     // sortOrder: "asc",//升序
    //     // sortOrder: "desc",//降序
    //     sidePagination: 'client',
    //     pageSize: 10,
    //     pageNumber: 1,
    //     pageList: [10, 20, 50, 100, 200, 500],
    //     // search: true,
    //     strictSearch: false,
    //     showColumns: true,
    //     showRefresh: false,
    //     showExport: false,
    //     uniqueId: "id",
    //     showToggle: false,
    //     cardView: false,
    //     // clickToSelect: true,
    //     showFooter: true,
    //     columns:
    //         [
    //             // {field: "checked", checkbox: true},
    //             {field: "staffId", title: "编号", align: "center", valign: "middle", sortable: "true"},
    //             {field: "staffName", title: "姓名", align: "center", valign: "middle", sortable: "true"},
    //             {
    //                 field: "staffType",
    //                 title: "端别",
    //                 align: "center",
    //                 valign: "middle",
    //                 sortable: "true",
    //                 formatter: formatType
    //             },
    //         ],
    // });
    Highcharts.chart('highchart',{
        chart: {
            // type: 'bar'
            type: 'column'
        },
        title: {
            text: '员工指标'
        },
        subtitle: {
            text: '暂无有效数据'
        },
        xAxis: {
            categories: staffs,
            crosshair: true,
            labels: {
                y: 15,
                autoRotation: [
                    -45
                ],
                distance: 15
            }
        },
        yAxis: {
            min: 0,
            title: {
                text: '数量（个）'
            }
        },
        pane: {
            size: '50%'
        },
        tooltip: {
            // head + 每个 point + footer 拼接成完整的 table
            headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
            pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
                '<td style="padding:0"><b>{point.y} 个</b></td></tr>',
            footerFormat: '</table>',
            shared: true,
            useHTML: true
        },
        plotOptions: {
            column: {
                dataLabels:{
                    enabled: true,
                },
                borderWidth: 0
            }
        },
        series: [{
            name: '新增bug',
            data: newBug
        }, {
            name: '解决bug',
            data: resolveBug
        }, {
            name: '开放',
            data: openBug
        }, {
            name: '进行中',
            data: inProcessBug
        }, {
            name: '重新打开',
            data: reopenBug
        }, {
            name: '已关闭',
            data: closedBug
        }]
    });

    function queryParams() {
        return {
            type: type
        }
    }
    function formatType(value) {
        switch (value) {
            case "test": return "测试端";
            case "web": return "Web端";
            case "client": return "客户端";
            case "server": return "服务端";
        }
    }

    function postRequest(type) {
        $.ajaxSettings.async = false;
        $.ajax({
            url: "/staff/FindStaffByType",
            method: 'post',
            contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
            cache: false,
            queryParams: queryParams(type),
            success: function (data) {
                response = data;
                for (var i = 0; i < response.length; i++) {
                    staffs = [];
                    staffs.push(response[i]["staffName"]);
                }
                console.log("response", response);
                console.log("staffs", staffs);
            }
        });

    }
}

function getDate() {
    var time = new Date();
    document.getElementById("start_time").value = addDate(time,-7);
    document.getElementById("end_time").value = addDate(time,+1);
    AddType();
}

function AddType() {
    $.ajax({
        url: "/staff/FindAllTypes",
        type: "post",
        success: function (data) {
            if (data.code != null) {
                var code = data.code;
                var message = data.message;
                if (code == "400") {
                    alert(message);
                    window.parent.location.href = "/login.html";
                }
                else if (code == "401") {
                    alert(message);
                    window.location.href = "/error/error.html";
                }
                return;
            }
            var items = "";
            var item = "";
            $.each(data, function (i, result) {
                item = "<option value='\"" + result + "\"'>" + result + "</option>";
                items += item;
            });
            items += "<option selected='selected' value=''>所有端别</option>";
            $('#department_select').html(items);
        }
    })
}

//返回上一级
function returnLastPage() {
    switch (flag) {
        case 0:
            break;
        case 1:
            break;
        case 2:
            search();
            break;
    }
}